package util;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class BaseDao <T>{
	Class<T> classt;
	Connection con;
	
	public BaseDao() {
		ParameterizedType pt=(ParameterizedType) this.getClass().getGenericSuperclass();
		classt=(Class<T>) pt.getActualTypeArguments()[0];
		con=DBUtil.getConnnection();
		
	}
	public List<T> getall(){
		List<T> list=new ArrayList<>();
		StringBuffer sql=new StringBuffer("select * from ").append(classt.getSimpleName());
		Statement stm;
		
		try {
			stm=con.createStatement();
			ResultSet rs=stm.executeQuery(sql.toString());
			Field []fd=classt.getDeclaredFields();
			while(rs.next())
			{
				T o=classt.newInstance();
				for(int i=0;i<fd.length;i++)
				{
					fd[i].setAccessible(true);
					String type=fd[i].getType().getName();
					Object val = rs.getObject(i + 1);
					fd[i].set(o, val);
				}list.add(o);
			}
		} catch (Exception e) {
			
		}
			
		
		return list;
		
		
	}
	// insert into tablename values(?,?,?.......)table名为class名
	public void insert(T o)
	{
		StringBuffer sql=new StringBuffer("insert into ").append(classt.getSimpleName()).append("valuse");
		Field fd[]=classt.getDeclaredFields();
		for (Field f : fd) {
			f.setAccessible(true);
			sql.append(",");
		}
		sql.deleteCharAt(sql.lastIndexOf(","));
		sql.append(")");
		try {
			PreparedStatement pstm=con.prepareStatement(sql.toString());
			for(int i=0;i<fd.length;i++)
			{
				fd[i].setAccessible(true);
				pstm.setObject(i+1, fd[i].get(o));
			}
			pstm.executeUpdate();
		} catch (Exception e) {
			
			e.printStackTrace();
		}
	}
	public void deletedata(T o) {
		StringBuffer sql = new StringBuffer("delete from ").append(classt.getSimpleName()+" ").append("where ");
		Field[] fd = classt.getDeclaredFields();
		fd[0].setAccessible(true);
		String filedname = fd[0].getName();
		sql.append(filedname + "=?");
		System.out.println(sql);
		try {
			PreparedStatement psm = con.prepareStatement(sql.toString());
			psm.setObject(1, fd[0].get(o));
			psm.executeUpdate();

		} catch (SQLException e) {

			e.printStackTrace();
		} catch (IllegalArgumentException e) {

			e.printStackTrace();
		} catch (IllegalAccessException e) {

			e.printStackTrace();
		}
	}
	//update student set name='tplink',age=16 where ID=23;
	
	public void update(T o,String country_id) throws IllegalArgumentException, IllegalAccessException {
			StringBuffer sql=new StringBuffer("update ").append(classt.getSimpleName()+" ").append(" set ");
			Field [] fd=classt.getDeclaredFields();
			for(int i=0;i<fd.length;i++)
			{
				fd[i].setAccessible(true);
				String fieldname=fd[i].getName();
				sql.append(fieldname+"="+"?"+",");
			}
			String fieldname=fd[0].getName();
			sql.deleteCharAt(sql.lastIndexOf(","));
			sql.append(" where "+fd[0].getName()+"='"+fd[0].get(o)+"'");//获取第一个成员值
			//System.out.println(sql);
			try {
				PreparedStatement ptsm=con.prepareStatement(sql.toString());
				ptsm.setObject(1, country_id);//把第一个问号替代为参数值
				for(int i=1;i<fd.length;i++)
				{
					ptsm.setObject(i+1, fd[i].get(o));//i+1表示问号的位置，替代的参数为fd[i].get(o)
		
				}
				ptsm.executeUpdate();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
			
		
	}

}
